package com.ruoyi.pay.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.pay.domain.PayInterfaceDefine;
import com.ruoyi.pay.mapper.PayInterfaceDefineMapper;
import com.ruoyi.pay.service.PayInterfaceDefineService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 支付接口定义表 服务实现类
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2021-04-27
 */

@Service
public class PayInterfaceDefineServiceImpl implements PayInterfaceDefineService {

    @Resource
    private PayInterfaceDefineMapper payInterfaceDefineMapper;


    @Override
    public PayInterfaceDefine getPayInterfaceDefine(PayInterfaceDefine payInterfaceDefine) {
        //查询返回
        PayInterfaceDefine interfaceDefine = payInterfaceDefineMapper
            .selectOne(PayInterfaceDefine.gw()
                .eq(PayInterfaceDefine::getIfCode, payInterfaceDefine.getIfCode()));
        return interfaceDefine;
    }

    @Override
    public TableDataInfo<PayInterfaceDefine> selectPayInterfaceList(PayInterfaceDefine payInterfaceDefine, PageQuery pageQuery) {
        Page<PayInterfaceDefine> page = payInterfaceDefineMapper.selectPageList(pageQuery.build(), this.buildQueryWrapper(payInterfaceDefine));
        return TableDataInfo.build(page);
    }


    private Wrapper<PayInterfaceDefine> buildQueryWrapper(PayInterfaceDefine payInterfaceDefine) {
        Map<String, Object> params = payInterfaceDefine.getParams();
        QueryWrapper<PayInterfaceDefine> wrapper = Wrappers.query();
        wrapper.eq(ObjectUtil.isNotNull(payInterfaceDefine.getIfCode()), "t.if_code", payInterfaceDefine.getIfCode());
        wrapper.eq(ObjectUtil.isNotNull(payInterfaceDefine.getIfName()), "t.if_name", payInterfaceDefine.getIfName());
        wrapper.eq(ObjectUtil.isNotNull(payInterfaceDefine.getState()), "t.state", payInterfaceDefine.getState());
        /*wrapper.eq(ObjectUtil.isNotNull(payInterfaceDefine.getIsIsvMode()), "t.is_isv_mode", payInterfaceDefine.getIsIsvMode());
        wrapper.eq(ObjectUtil.isNotNull(payInterfaceDefine.getIsMchMode()), "t.is_mch_mode", payInterfaceDefine.getIsMchMode());*/
        wrapper.eq(ObjectUtil.isNotNull(payInterfaceDefine.getConfigPageType()), "t.config_page_type", payInterfaceDefine.getConfigPageType());
        wrapper.eq(ObjectUtil.isNotNull(payInterfaceDefine.getRemark()), "t.remark", payInterfaceDefine.getRemark());
        wrapper.between(params.get("beginTime") != null && params.get("endTime") != null,
            "t.create_time", params.get("beginTime"), params.get("endTime"));
        return wrapper;
    }

}
